package com.tour.tour.utils;

import cn.hutool.json.JSONObject;
import com.tour.tour.dto.VerifyCodeDTO;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

/**
 * @author YounGCY
 * @date 2023-5-15 下午 6:00
 * @description
 */
public class VerifyCodeUtil {
    public static VerifyCodeDTO getCode() {
        // 创建 RestTemplate 实例
        RestTemplate restTemplate = new RestTemplate();

        // 发送 GET 请求并接收响应
        ResponseEntity<String> response = restTemplate.getForEntity("https://www.mxnzp.com/api/verifycode/code?len=5&type=0&app_id=ootv7bgasirhqith&app_secret=UEVNOUp1SklPaGY1K21nbElvOUJwZz09", String.class);

        // 解析 JSON 数据
        JSONObject json = new JSONObject(response.getBody());
        JSONObject data = json.getJSONObject("data");
        String verifyCode = data.getStr("verifyCode");
        String verifyCodeImgUrl = data.getStr("verifyCodeImgUrl");
        
        
        VerifyCodeDTO verifyCodeDTO = new VerifyCodeDTO();
        verifyCodeDTO.setCode(verifyCode);
        verifyCodeDTO.setCodeUrl(verifyCodeImgUrl);
        
        return verifyCodeDTO;
    }
}
